package 多线程.批处理任务工具;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @
 * @date 2025/07/01
 */
public class CompletableFutureDemo {

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(3);

        long s = System.currentTimeMillis();
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }, executorService);
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }, executorService);
        CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }, executorService);

        CompletableFuture.allOf(future1, future2, future3).join();

        long e = System.currentTimeMillis();

        System.out.println(e - s);
    }

}
